﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace MultiWallpaperManager
{
	public class GeneralConfigSection : ConfigurationSection
	{
		public const string Name = "general";

		private const string FolderPathString = "folderpath";
		private const string NumScreensString = "numscreens";
		private const string IntervalString = "interval";
		private const string StartupString = "startup";
		private const string ChangeWhenRunString = "changewhenrun";

		[ConfigurationProperty(FolderPathString, DefaultValue = "")]
		public string FolderPath
		{
			get	{ return (string)this[FolderPathString]; }
			set	{ this[FolderPathString] = value;	}
		}

		[ConfigurationProperty(NumScreensString, DefaultValue = 0)]
		public int NumScreens
		{
			get	{ return (int)this[NumScreensString]; }
			set	{ this[NumScreensString] = value;	}
		}

		[ConfigurationProperty(IntervalString, DefaultValue = 10)]
		public int Interval
		{
			get { return (int)this[IntervalString]; }
			set { this[IntervalString] = value; }
		}
		
		[ConfigurationProperty(StartupString, DefaultValue = false)]
		public bool Startup
		{
			get { return (bool)this[StartupString]; }
			set { this[StartupString] = value; }
		}

		[ConfigurationProperty(ChangeWhenRunString, DefaultValue = false)]
		public bool ChangeWhenRun
		{
			get { return (bool)this[ChangeWhenRunString]; }
			set { this[ChangeWhenRunString] = value; }
		}
	}

	public class ScreenConfigSection : ConfigurationSection
	{
		private const string ImageFileNameString = "filename";
		private const string RandomImageString = "randomimage";

		public static string MakeName(int id)
		{
			return "screen" + id;
		}

		[ConfigurationProperty(ImageFileNameString, DefaultValue = "")]
		public string ImageFileName
		{
			get { return (string)this[ImageFileNameString]; }
			set { this[ImageFileNameString] = value; }
		}

		[ConfigurationProperty(RandomImageString, DefaultValue = false)]
		public bool Random
		{
			get { return (bool)this[RandomImageString]; }
			set { this[RandomImageString] = value; }
		}
	}

	public class Config
	{
		public static void Save(GeneralConfigSection general, ScreenConfigSection[] screens)
		{
			// open config file
			Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

			config.Sections.Clear();

			general.SectionInformation.ForceSave = true;
			config.Sections.Add(GeneralConfigSection.Name, general);

			// per screen
			for (int i = 0; i < screens.Length; i++)
			{
				ScreenConfigSection screen = screens[i];

				screen.SectionInformation.ForceSave = true;
				config.Sections.Add(ScreenConfigSection.MakeName(i), screen);				
			}

			// save
			config.Save(ConfigurationSaveMode.Full);
		}

		public static bool Load(out GeneralConfigSection general, out ScreenConfigSection[] screens)
		{
			try
			{
				// open config file
				Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

				if (config.Sections[GeneralConfigSection.Name] == null)
				{
					throw new Exception("config file doesn't exist");
				}

				// read
				general = config.Sections[GeneralConfigSection.Name] as GeneralConfigSection;

				screens = new ScreenConfigSection[general.NumScreens];

				for (int i = 0; i < general.NumScreens; i++)
				{
					screens[i] = config.Sections[ScreenConfigSection.MakeName(i)] as ScreenConfigSection;
				}
			}
			catch (Exception)
			{
				general = null;
				screens = null;

				return false;
			}

			return true;
		}
	}
}
